package com.mcxx.modules.extremepoverty.service.impl;

import com.mcxx.modules.extremepoverty.service.ExFundPayService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.fundpay.dao.read.FundPayMemberReadDao;
import com.mcxx.modules.miniSalvation.fundpay.dto.*;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayFamilyParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayMemberParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayParam;
import com.mcxx.modules.miniSalvation.fundpay.service.read.CustomFundPayMemberReadService;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayFamilyReadService;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayMemberReadService;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayReadService;
import com.mcxx.modules.miniSalvation.fundpay.service.write.FundPayFamilyWriteService;
import com.mcxx.modules.miniSalvation.fundpay.service.write.FundPayMemberWriteService;
import com.mcxx.modules.miniSalvation.fundpay.service.write.FundPayWriteService;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.util.Constant;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


@Slf4j
@Service
@TaTransactional
public class ExFundPayServiceImpl extends BaseService implements ExFundPayService {
    @Autowired
    private FundPayReadService fundPayReadService;
    @Autowired
    private FundPayMemberReadService fundPayMemberReadService;
    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private CustomFundPayMemberReadService customFundPayMemberReadService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;
    @Autowired
    private FundPayFamilyReadService fundPayFamilyReadService;
    @Autowired
    private FundPayMemberReadDao fundPayMemberReadDao;



    @Override
    public Page getSupportPayMemberList (FundPayParam param, PageParam pageParam){
        Page supportPayMemberList = fundPayMemberReadService.getSupportPayMemberList(param, pageParam);
        List<FundPayMemberListDTO> list = supportPayMemberList.getList();
        list.forEach(supportEnt ->{


            BigDecimal countPay = supportEnt.getNursingPay().add(supportEnt.getBasicLifePay());
            supportEnt.setFundNumber(countPay.doubleValue());
        });
        supportPayMemberList.setList(list);
        return supportPayMemberList;
    }


    @Override
    public Page getCarePayMemberList (FundPayParam param, PageParam pageParam){
        return fundPayMemberReadService.getCarePayMemberList(param,pageParam);
    }


    @Override
    public ExcelInfo exportExcelExFundPay(String exportType, List<String> orderIdList, FundPayParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        if(param.getPayDate() != null && param.getPayDate().length() == 7){
            param.setPayDate(param.getPayDate() +"-01");
        }
        if("1".equals(exportType)){
            String name = "资金发放名册信息";
            if (Constant.BusinessCode.TEKUN.equals(param.getBusinessType())) {
                if (Constant.CapitalFundPayCostType.NURSING.equals(param.getCostType())) {
                    name = "特困照料护理费资金发放名册信息";
                } else {
                    name = "特困基本生活费资金发放名册信息";
                }
            } else {
                name = "保障金资金发放名册信息";
            }

            param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayExcelDto> list = fundPayReadService.exportFundPayList(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayExcelDto.class,list,name + ".xls",
                    name, name,false,"payDate","payType","commitDate",
                    "isAuto","payWay","households","standType","basic","nursing");
        }else if("2".equals(exportType)){

            param.setFundType(Constant.FundType.STAND_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayExcelDto> list = fundPayReadService.exportFundPayList(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayExcelDto.class,list,"标准补发名册信息.xls",
                    "标准补发名册列表","标准补发名册列表",false,"replacementType","replacementMonth",
                    "remark","payWay","households","standType");
        }else if("3".equals(exportType)){

            param.setFundType(Constant.FundType.ALLOWANCE_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayExcelDto> list = fundPayReadService.exportFundPayList(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayExcelDto.class,list,"补贴发放名册信息.xls",
                    "补贴发放名册列表","补贴发放名册列表",false,"itemType","grantObject","remark",
                    "grantStandard","grantYears","replacementMonth","remark","households","standType");
        }

        return excelInfo;
    }


    @Override
    public ExcelInfo exportExcelCapitalObjectList(FundPayFamilyParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);


        param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);

        List<FundPayFamilyExcelDTO> list  = fundPayFamilyReadService.exportFundPayFamilyList(param);

        excelInfo = exportExcelService.exportOrgOpLogs(FundPayFamilyExcelDTO.class,list,"特困照料护理费发放对象信息.xls",
                "特困照料护理费发放对象信息","特困照料护理费发放对象信息",false,null);


        return excelInfo;
    }


    @Override
    public ExcelInfo exportExcelExFundPayMember(String exportType, List<String> orderIdList, FundPayParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        if(param.getPayDate() != null){
            param.setPayDate(param.getPayDate() +"-01");
        }
        String titleName = "";
        if("1".equals(exportType)){

            titleName = "供养金放明细列表";
            param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayMemberListDTO> list = fundPayMemberReadService.getSupportPayMember(param);
            list.forEach(supportEnt ->{


                BigDecimal countPay = supportEnt.getNursingPay().add(supportEnt.getBasicLifePay());
                supportEnt.setFundNumber(countPay.doubleValue());
            });

            excelInfo = exportExcelService.exportOrgOpLogs(FundPayMemberListDTO.class,list,"供养金放明细信息.xls",
                    titleName,"供养金放明细列表",false,"basicLifePay","salvationNo","isHelpPoor","docHelpPoor");
        }else if("2".equals(exportType)){

            titleName = "标准补发明细列表";
            param.setFundType(Constant.FundType.STAND_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayMemberExcelDTO> list = fundPayMemberReadService.exportExcelFundPayMember(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayMemberExcelDTO.class,list,"标准补发明细信息.xls",
                    titleName,"标准补发明细列表",false,"standTypet");
        }else if("3".equals(exportType)){

            titleName = "补贴发放明细列表";
            param.setFundType(Constant.FundType.ALLOWANCE_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayMemberExcelDTO> list = fundPayMemberReadService.exportExcelFundPayMember(param);
            excelInfo = exportExcelService.exportOrgOpLogs(FundPayMemberExcelDTO.class,list,"补贴发放明细信息.xls",
                    titleName,"补贴发放明细列表",false,"SubsidiesType","grantObject","grantStandard");
        }else if("4".equals(exportType)){

            titleName = "自定义发放明细列表";
            param.setFundType(Constant.FundType.CUSTOM_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayMemberExcelDTO> list = fundPayMemberReadService.exportExcelFundPayMember(param);

            excelInfo = exportExcelService.exportOrgOpLogs(FundPayMemberExcelDTO.class,list,"自定义发放明细信息.xls",
                    titleName,"自定义发放明细列表",false,null);
        }else if("5".equals(exportType)){

            titleName = "护理费放明细列表";
            param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);
            param.setIdsList(orderIdList);
            List<FundPayMemberListDTO> list = fundPayMemberReadService.getSupportPayMember(param);
            list.forEach(supportEnt ->{


                BigDecimal countPay = supportEnt.getNursingPay().add(supportEnt.getBasicLifePay());
                supportEnt.setFundNumber(countPay.doubleValue());
            });

            excelInfo = exportExcelService.exportOrgOpLogs(FundPayMemberListDTO.class,list,"护理费放明细信息.xls",
                    titleName,"护理费放明细列表",false,"nursingPay","careName","careIdCard");
        }


        return excelInfo;
    }



    @Override
    public ExcelInfo exportDisperseExcelCapitalCareObjectList(FundPayMemberParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);

        param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);
        List<FundPayMemberDTO> list  = null;

        if (Constant.recevierType.MECHANISM.equals(param.getRecevierType())) {
            list = fundPayMemberReadDao.getDisperseCapitalObjectOrganizationList(param);
        } else {
            list = fundPayMemberReadDao.getDisperseCapitalObjectList(param);
        }

        List<CareObjectFundPayExcelDTO> results = CareObjectFundPayExcelDTO.convertExcelDTO(list);

        excelInfo = exportExcelService.exportOrgOpLogs(CareObjectFundPayExcelDTO.class,results,"分散供养护理费照料对象信息.xls",
                "分散供养护理费照料对象信息","分散供养护理费照料对象信息",false,null);


        return excelInfo;
    }



    @Override
    public ExcelInfo exportDisperseExcelCapitalCarePersonList(FundPayMemberParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);

        param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);
        List<FundPayMemberDTO> list  = null;

        if (Constant.recevierType.MECHANISM.equals(param.getRecevierType())) {
            list = fundPayMemberReadDao.getCareOrganizationList(param);
        } else {
            list = fundPayMemberReadDao.getCarePersonList(param);
        }

        List<CarePersonFundPayExcelDTO> results = CarePersonFundPayExcelDTO.convertExcelDTO(list);

        excelInfo = exportExcelService.exportOrgOpLogs(CarePersonFundPayExcelDTO.class,results,"分散供养护理费照料人信息.xls",
                "分散供养护理费照料人列表","分散供养护理费照料人列表",false,null);


        return excelInfo;
    }

    @Override
    public ExcelInfo exportFocusCapitalObjectList(FundPayFamilyParam param, UserAccountVo accountVo) {
        ExcelInfo excelInfo = null;
        String title = "特困基本生活费发放对象信息";

        param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);
        List<FundPayMemberDTO> list = fundPayFamilyReadService.getFocusCapitalObjectListExcel(param);
        List<FundPayFocusMemberExcelDTO> list1 = new ArrayList<>();
        for (FundPayMemberDTO obj: list) {
            FundPayFocusMemberExcelDTO excelDTO = new FundPayFocusMemberExcelDTO();
            BeanUtils.copyProperties(obj, excelDTO);
            list1.add(excelDTO);
        }
        if (Constant.CapitalFundPayCostType.NURSING.equals(param.getCostType())) {
            title = "特困照料护理费发放对象信息";
        }
        excelInfo = exportExcelService.exportOrgOpLogs(FundPayFocusMemberExcelDTO.class,list1,title + ".xls",
                title,title,false,null);

        return excelInfo;
    }
}
